package com.xyhuo.leetcode.january;

import com.xyhuo.leetcode.common.Node;

import java.util.HashMap;
import java.util.Map;

/**
 * 填充每个节点的下一个右侧节点指针
 * https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/
 *
 * @time 2024/1/11
 */
public class Connect {

    public Node connect(Node root) {
        search(root, 0);
        return root;
    }

    Map<Integer, Node> map = new HashMap<>();

    private void search(Node root, int step) {
        if (null == root) {
            return;
        }
        if (map.containsKey(step)) {
            map.get(step).next = root;
        }
        map.put(step, root);
        search(root.left, step + 1);
        search(root.right, step + 1);
    }
}
